package com.sxm.yiwei.activitis;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.BookContentLinkAdapter;
import com.sxm.yiwei.adapters.FWContentImgAdapter;
import com.sxm.yiwei.beans.CryBookSq;
import com.sxm.yiwei.beans.HotContentRecever;
import com.sxm.yiwei.tools.CallPhone;
import com.sxm.yiwei.tools.CastTime;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.tools.ImgSwitcherActivity;
import com.sxm.yiwei.tools.MySQManager;
import com.sxm.yiwei.tools.MySqHelper;
import com.sxm.yiwei.tools.REXTools;
import com.sxm.yiwei.tools.YiViMapActivity;
import com.sxm.yiwei.utils.BookStateChangeUtils;
import com.sxm.yiwei.utils.InternetAddressUtis;
import com.sxm.yiwei.views.MyGridView;
import com.sxm.yiwei.views.MyListview;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import static android.R.attr.name;
import static android.R.attr.switchMinWidth;

public class BookContentHotActivity extends BaseActivity implements View.OnClickListener{

    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String id;
    private int type;
    private ImageView hotback;
    private TextView hotamount;
    private LinearLayout hotaddn;
    private TextView hotaddt;
    private LinearLayout hotcase;
    private TextView hotcaset;
    private TextView hotWeight;
    private TextView hotbao;
    private TextView alldistance;
    private TextView hotsendone;
    private TextView hotcode;
    private TextView hotintro;
    private LinearLayout hotimgs;
    private TextView sendadd;
    private MyListview hotreceveradd;
    private TextView metosend;
    private TextView sendtoend;
    private TextView hotsendtime;
    private TextView gettime;
    private TextView hotyivier;
    private TextView hotbookstate;
    private Button weiyixai;
    private Button call;
    private Button map;
    private Button appeal;
    private Button over;
    private Button pickup;
    private Button cancel;
    private Button crybook;
    private Button select;
    private String TAG ="hot";
    private String latitude;
    private String longitude;
    private MyGridView gridimgs;
    private float distan;

    private Handler h = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Bundle data = msg.getData();
                    String dis = data.getString("dis");
                    Double ddis = Double.valueOf(dis);
                    DecimalFormat df   = new DecimalFormat("######0.00");
                    double v = ddis / 1000;
                    metosend.setText(df.format(v)+"公里");

                    break;
                case 2:
                    Bundle data2 = msg.getData();
                    String dis2 = data2.getString("dist");
                    Double ddis2 = Double.valueOf(dis2);
                    DecimalFormat df2   = new DecimalFormat("######0.00");
                    double v2 = ddis2 / 1000;
                    sendtoend.setText(df2.format(v2)+"公里");
                    alldistance.setText(df2.format(v2)+"公里");
                    break;
            }
        }
    };
    private MyListview receverLink;
    private String courierMobile;
    private String consignorTel;
    private ImageView xiaofei;
    private ImageView jiaji;
    private ImageView jiajia;
    private String orderId;
    private List<HotContentRecever> receverList;
    private String consignorLat;
    private String consignorLong;
    private BookStateChangeUtils bookStateChangeUtils;
    private ImageView backmain;
    private String uid;

    /**
     * 急件要发订单详情
     *
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_content_hot);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");
        latitude = sharedPreferences.getString("latitude", "");
        longitude = sharedPreferences.getString("longitude", "");
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        uid = intent.getStringExtra("uid");
        type = intent.getIntExtra("TYPE", 1);
        initHotView();
        toViewButton(type);
        getBookContent();

    }
        // 底部按钮显示
    private void toViewButton(int t) {
        switch (t){
            case 2001:// 发单人-待抢单
                map.setVisibility(View.VISIBLE);
                cancel.setVisibility(View.VISIBLE);
                call.setVisibility(View.GONE);
                weiyixai.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.GONE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.GONE);
                break;
            case 2002:// 待选定
                map.setVisibility(View.VISIBLE);
                cancel.setVisibility(View.VISIBLE);
                select.setVisibility(View.VISIBLE);
                weiyixai.setVisibility(View.GONE);
                call.setVisibility(View.GONE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.GONE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.GONE);
                break;
            case 2003:// 待取货
                map.setVisibility(View.VISIBLE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.VISIBLE);
                call.setVisibility(View.VISIBLE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.VISIBLE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.GONE);

                break;
            case 2004:
                map.setVisibility(View.VISIBLE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.VISIBLE);
                call.setVisibility(View.VISIBLE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.VISIBLE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.GONE);
                break;
            case 2005:
                map.setVisibility(View.GONE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.VISIBLE);
                call.setVisibility(View.VISIBLE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.VISIBLE);
                over.setVisibility(View.VISIBLE);
                pickup.setVisibility(View.GONE);
                break;
            case 2006:
                map.setVisibility(View.GONE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.GONE);
                call.setVisibility(View.GONE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.VISIBLE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.GONE);
                break;
            case 1001: // 一喂员 待选定
                map.setVisibility(View.VISIBLE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.GONE);
                call.setVisibility(View.GONE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.GONE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.GONE);
                break;
            case 1002: // 待取货
                map.setVisibility(View.VISIBLE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.VISIBLE);
                call.setVisibility(View.VISIBLE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.VISIBLE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.VISIBLE);
                break;
            case 1003:// 配送中
                map.setVisibility(View.VISIBLE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.VISIBLE);
                call.setVisibility(View.VISIBLE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.VISIBLE);
                over.setVisibility(View.VISIBLE);
                pickup.setVisibility(View.GONE);
                break;
            case 1004: // 待完结
                map.setVisibility(View.GONE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.GONE);
                call.setVisibility(View.GONE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.VISIBLE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.GONE);
                break;
            case 1005: // 已完结
                map.setVisibility(View.GONE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.GONE);
                call.setVisibility(View.GONE);
                crybook.setVisibility(View.GONE);
                appeal.setVisibility(View.VISIBLE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.GONE);
                break;
            case 3000:
                map.setVisibility(View.GONE);
                cancel.setVisibility(View.GONE);
                select.setVisibility(View.GONE);
                weiyixai.setVisibility(View.VISIBLE);
                call.setVisibility(View.GONE);
                crybook.setVisibility(View.VISIBLE);
                appeal.setVisibility(View.GONE);
                over.setVisibility(View.GONE);
                pickup.setVisibility(View.GONE);
                break;
        }
    }

    private void initHotView() {
        backmain = (ImageView) findViewById(R.id.bookhot_backmain);
        xiaofei = (ImageView) findViewById(R.id.contenthot_xiaofei);
        jiaji = (ImageView) findViewById(R.id.contenthot_jiaji);
        jiajia = (ImageView) findViewById(R.id.contenthot_jiajia);
        gridimgs = (MyGridView) findViewById(R.id.contenthot_introimgs);
        hotback = (ImageView) findViewById(R.id.content_hotback);
        hotamount = (TextView) findViewById(R.id.contenthot_amount);
        hotaddn = (LinearLayout) findViewById(R.id.contenthot_addnumber);
        hotaddt = (TextView) findViewById(R.id.contenthot_addpricenumbert);
        hotcase = (LinearLayout) findViewById(R.id.contenthot_case);
        hotcaset = (TextView) findViewById(R.id.contenthot_caset);
        hotWeight = (TextView) findViewById(R.id.contenthot_weight);
        hotbao = (TextView) findViewById(R.id.contenthot_bao);
        alldistance = (TextView) findViewById(R.id.contenthot_alldistance);
        hotsendone = (TextView) findViewById(R.id.contenthot_sendone);
        hotcode = (TextView) findViewById(R.id.contenthot_code);
        hotintro = (TextView) findViewById(R.id.contenthot_intro);
        hotimgs = (LinearLayout) findViewById(R.id.contenthot_imgs);
        sendadd = (TextView) findViewById(R.id.contenthot_sendadd);
        hotreceveradd = (MyListview) findViewById(R.id.contenthot_receveradd);
        metosend = (TextView) findViewById(R.id.contenthot_metosend);
        sendtoend = (TextView) findViewById(R.id.contenthot_sendtoend);
        hotsendtime = (TextView) findViewById(R.id.contenthot_sendtime);
        gettime = (TextView) findViewById(R.id.contenthot_gettime);
        hotyivier = (TextView) findViewById(R.id.contenthot_yivier);
        hotbookstate = (TextView) findViewById(R.id.contenthot_bookstate);
        weiyixai = (Button) findViewById(R.id.contenthot_weiyixia);
        call = (Button) findViewById(R.id.contenthot_call);
        map = (Button) findViewById(R.id.contenthot_map);
        appeal = (Button) findViewById(R.id.contenthot_appeal);
        over = (Button) findViewById(R.id.contenthot_over);
        pickup = (Button) findViewById(R.id.contenthot_pickup);
        cancel = (Button) findViewById(R.id.contenthot_cancel);
        crybook = (Button) findViewById(R.id.contenthot_crybook);
        select = (Button) findViewById(R.id.contenthot_select);

        // 点击事件
        backmain.setOnClickListener(this);
        hotback.setOnClickListener(this);
        weiyixai.setOnClickListener(this);
        call.setOnClickListener(this);
        map.setOnClickListener(this);
        appeal.setOnClickListener(this);
        over.setOnClickListener(this);
        pickup.setOnClickListener(this);
        cancel.setOnClickListener(this);
        crybook.setOnClickListener(this);
        select.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        bookStateChangeUtils = new BookStateChangeUtils();
        switch (v.getId()){
            case R.id.bookhelp_backmain:
                startActivity(new Intent(BookContentHotActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.content_hotback:
                finish();
                break;
            case R.id.contenthot_weiyixia:
                Toast.makeText(this, "功能建设中，敬请期待", Toast.LENGTH_SHORT).show();
                break;
            case R.id.contenthot_call:
                String phoneno = null;
                if (type>1000&type<2000){
                    phoneno = consignorTel;
                }else {
                    phoneno = courierMobile;
                }
                CallPhone.callPhone(BookContentHotActivity.this,phoneno);
                break;
            case R.id.contenthot_map:
                Intent i = new Intent(BookContentHotActivity.this,YiViMapActivity.class);
                    Double consigneeLate = receverList.get(0).getConsigneeLat();
                    Double consigneeLonge = receverList.get(0).getConsigneeLong();
                    i.putExtra("mEndLatitude",consigneeLate);
                    i.putExtra("mEndLongitude",consigneeLonge);
                    i.putExtra("mStartLatitude",Double.valueOf(consignorLat));
                    i.putExtra("mStartLongitude",Double.valueOf(consignorLong));
                    startActivity(i);
                break;
            case R.id.contenthot_appeal:
                Intent appealintent = new Intent(BookContentHotActivity.this,BookAppealActivity.class);
                appealintent.putExtra("id",orderId);
                startActivity(appealintent);
                break;
            case R.id.contenthot_over:
                if (type>1000&type<2000){

                    Intent i1 = new Intent(BookContentHotActivity.this,PickUpActivity.class);
                    i1.putExtra("id",orderId);
                    i1.putExtra("from","2");
                    startActivity(i1);
                }else {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(BookContentHotActivity.this);
                    View view = LayoutInflater.from(BookContentHotActivity.this).inflate(R.layout.cancel_servicedialog, null);
                    Button cancelbt = (Button) view.findViewById(R.id.cancel_cancelbt);
                    Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
                    TextView t1 = (TextView) view.findViewById(R.id.cancel_t1);
                    TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
                    builder.setView(view);
                    t2.setText("您确定要完结此单吗？");
                    surebt.setText("确定");
                    final AlertDialog overDialog = builder.create();
                    cancelbt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            overDialog.dismiss();
                        }
                    });
                    surebt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            bookStateChangeUtils.overBook(orderId);
                            //overBook();
                            overDialog.dismiss();

                        }
                    });
                    overDialog.show();

                }

                break;
            case R.id.contenthot_pickup:
                //取货
                Intent pickintent = new Intent(BookContentHotActivity.this,PickUpActivity.class);
                pickintent.putExtra("id",id);
                pickintent.putExtra("from","1");
                startActivity(pickintent);
                break;
            case R.id.contenthot_cancel:
                bookStateChangeUtils.cancelBook(orderId);
                //cancelBook(orderId);
                break;
            case R.id.contenthot_select: // 选定一喂员

                    Intent intent1 = new Intent(BookContentHotActivity.this,YiViListActivity.class);
                    intent1.putExtra("id",orderId);
                    startActivity(intent1);

                break;
            case R.id.contenthot_crybook: // 抢单
                if (uid.equals(userId)){
                    Toast.makeText(this, "这是您自己的订单，自己不可以抢哦", Toast.LENGTH_SHORT).show();
                }else {
                    cryBook(id);
                }
                break;
        }
    }
        // 订单信息
    private void getBookContent() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.BOOKCONTENT);
        params.addHeader("Content-Type", "application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId", id);
        params.addParameter("params", j.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d(TAG, "onSuccess: 几件--"+result);
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                String consignorAddr = obj1.getString("consignorAddr");// 发货地
                String consignorHous = obj1.getString("consignorHous");// 发货地门牌号
                JSONArray consignee = obj1.getJSONArray("consignee");// 收货人信息
                String appointTime = obj1.getString("appointTime");// 预约时间
                String endTime = obj1.getString("endTime");// 结束时间
                String tip = obj1.getString("tip");// 小费
                String urgent = obj1.getString("urgent"); // 加急
                // 经度
                consignorLat = obj1.getString("consignorLat");
                // 纬度
                consignorLong = obj1.getString("consignorLong");
                String consignorName = obj1.getString("consignorName"); // 发货人姓名

                // 发货人电话
                consignorTel = obj1.getString("consignorTel");
                String insurance = obj1.getString("insurance"); // 保价
                String intro = obj1.getString("intro"); // 订单简介
                final JSONArray introImage1 = obj1.getJSONArray("introImage");//简介图片
                String makeupCount = obj1.getString("makeupCount"); // 加价次数
                String orderCode = obj1.getString("orderCode"); // 订单号
                // 订单id
                orderId = obj1.getString("orderId");
                String publishTime = obj1.getString("publishTime"); // 发单时间
                String sendStatus = obj1.getString("sendStatus"); // 订单状态
                String totalAmount = obj1.getString("totalAmount"); // 订单价格
                String type1 = obj1.getString("type"); // 订单类型
                String weight = obj1.getString("weight"); // 重量

                // 一喂员电话
                courierMobile = obj1.getString("courierMobile");
                String courierNickname = obj1.getString("courierNickname"); // 一喂员
                // 设置数据
                hotamount.setText(totalAmount);
                hotbao.setText(insurance+" 元");
                hotintro.setText(intro);
                hotcode.setText(orderCode);
               if (type==3000){
                   hotsendone.setText(consignorName+" "+ REXTools.hidePhone(consignorTel));
               }else {
                   hotsendone.setText(consignorName+" "+consignorTel);
               }

                if (consignorHous!=null){
                    if (type==3000){
                        sendadd.setText(REXTools.hideAddress(consignorAddr)+"***");
                    }else {
                        sendadd.setText(consignorAddr+consignorHous);
                    }

                }else {
                    if (type==3000){
                        sendadd.setText(REXTools.hideAddress(consignorAddr));
                    }else {
                        sendadd.setText(consignorAddr);
                    }

                }

                hotsendtime.setText(CastTime.CastTime(Long.valueOf(publishTime)));
                gettime.setText(CastTime.startTime(Long.valueOf(appointTime)));
                if (courierNickname!=null){// 显示一喂员
                    hotyivier.setText(courierNickname+" "+courierMobile);
                }else {
                    hotyivier.setText(" ");
                }
                if (endTime!=null){
                    boolean b = CastTime.bookEndTime(Long.valueOf(endTime));
                    if (b){
                        appeal.setVisibility(View.GONE);
                    }else {

                    }
                }else {

                }
                if (weight.equals("0")){
                    hotWeight.setText("0-2kg");
                }else if (weight.equals("1")){
                    hotWeight.setText("2-5kg");
                }else if (weight.equals("2")){
                    hotWeight.setText("5-10kg");
                }else if (weight.equals("3")){
                    hotWeight.setText("10-20kg");
                }
                if (makeupCount.equals("0")){ // 加价次数为 0

                }else { // 不为 0
                    hotaddn.setVisibility(View.VISIBLE);
                    hotaddt.setText(makeupCount);
                    jiajia.setVisibility(View.VISIBLE);
                }

                if (tip.equals("0")){// 小费

                }else {
                    switch (Integer.valueOf(tip)){
                        case 1:
                            hotcaset.setText("5");
                            break;
                        case 2:
                            hotcaset.setText("10");
                            break;
                        case 3:
                            hotcaset.setText("20");
                            break;
                    }
                    hotcase.setVisibility(View.VISIBLE);
                    xiaofei.setVisibility(View.VISIBLE);
                }
                if (urgent.equals("true")){// 加急
                    jiaji.setVisibility(View.VISIBLE);
                }else {
                    jiaji.setVisibility(View.GONE);
                }
                if (sendStatus.equals("1")) {
                    hotbookstate.setText("待抢单");
                } else if (sendStatus.equals("2")) {
                    hotbookstate.setText("待选定");
                } else if (sendStatus.equals("3")) {
                    hotbookstate.setText("待取货");
                } else if (sendStatus.equals("4")) {
                    hotbookstate.setText("配送中");
                } else if (sendStatus.equals("5")) {
                    hotbookstate.setText("待完结");
                } else if (sendStatus.equals("6")) {
                    hotbookstate.setText("已完结");
                } else if (sendStatus.equals("0")) {
                    hotbookstate.setText("未支付订单");
                } else if (sendStatus.equals("-1")) {
                    hotbookstate.setText("已投诉");
                } else if (sendStatus.equals("-2")) {
                    hotbookstate.setText("订单已取消");
                }
                // 收货人信息集合
                receverList = new ArrayList<HotContentRecever>();
                for (int i = 0; i < consignee.size(); i++) {
                    HotContentRecever hotContentRecever = new HotContentRecever();
                    JSONObject objRecever = JSON.parseObject(String.valueOf(consignee.get(i)));
                    String consigneeAddr = objRecever.getString("consigneeAddr");
                    String consigneeHous = objRecever.getString("consigneeHous");
                    String consigneeLat = objRecever.getString("consigneeLat");
                    String consigneeLong = objRecever.getString("consigneeLong");
                    String consigneeName = objRecever.getString("consigneeName");
                    String consigneeTel = objRecever.getString("consigneeTel");
                    hotContentRecever.setConsigneeAddr(consigneeAddr);
                    hotContentRecever.setConsigneeHous(consigneeHous);
                    hotContentRecever.setConsigneeLat(Double.valueOf(consigneeLat));
                    hotContentRecever.setConsigneeLong(Double.valueOf(consigneeLong));
                    hotContentRecever.setConsigneeName(consigneeName);
                    hotContentRecever.setConsigneeTel(consigneeTel);
                    receverList.add(hotContentRecever);
                }
                final BookContentLinkAdapter adapter = new BookContentLinkAdapter(BookContentHotActivity.this, receverList);
                hotreceveradd.setAdapter(adapter);
                hotreceveradd.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        HotContentRecever item = (HotContentRecever) adapter.getItem(position);
                        String name = item.getConsigneeName();
                        final String phone = item.getConsigneeTel();
                        CallPhone.callPhone(BookContentHotActivity.this,phone);

                    }
                });
                // 测距
                LatLonPoint sl = new LatLonPoint(Double.valueOf(latitude),Double.valueOf(longitude));
                LatLonPoint el = new LatLonPoint(Double.valueOf(consignorLat),Double.valueOf(consignorLong));
                LatLonPoint eL= new LatLonPoint(receverList.get(0).getConsigneeLat(), receverList.get(0).getConsigneeLong());

                getDispatchingstart(sl,el);
                getDispatchingend(el,eL);


                if (introImage1.size()==0){

                }else {
                    hotimgs.setVisibility(View.VISIBLE);
                    FWContentImgAdapter imgAdapter = new FWContentImgAdapter(BookContentHotActivity.this,introImage1);
                    gridimgs.setAdapter(imgAdapter);
                    int itemWidth = (int)(125 * gridimgs.getResources().getDisplayMetrics().density);
                    int itemSize = introImage1.size();
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemSize * itemWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
                    gridimgs.setLayoutParams(params);
                    gridimgs.setNumColumns(itemSize);
                    gridimgs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent in = new Intent(BookContentHotActivity.this, ImgSwitcherActivity.class);
                            in.putExtra("position",position);
                            in.putExtra("imgs",introImage1);
                            startActivity(in);
                        }
                    });

                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d(TAG, "onError: 几件--"+ex);
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void getDispatchingstart(LatLonPoint s , LatLonPoint e) {
        RouteSearch search = new RouteSearch(BookContentHotActivity.this);
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(s,e);
        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo,RouteSearch.DrivingShortDistance,null,null,"");
        search.calculateDriveRouteAsyn(query);
        search.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
            @Override
            public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

            }

            @Override
            public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
                // 拿结果
                List<DrivePath> paths = driveRouteResult.getPaths();
                for (int i1 = 0; i1 < paths.size(); i1++) {
                    distan = paths.get(i1).getDistance();

                    Message m = new Message();
                    m.what=1;
                    Bundle b = new Bundle();
                    b.putString("dis", distan +"");
                    m.setData(b);
                    h.sendMessage(m);
                }
            }

            @Override
            public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

            }

            @Override
            public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

            }
        });

    }
    private void getDispatchingend(LatLonPoint s ,LatLonPoint e) {

        RouteSearch search = new RouteSearch(BookContentHotActivity.this);
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(s,e);
        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo,RouteSearch.DrivingShortDistance,null,null,"");
        search.calculateDriveRouteAsyn(query);
        search.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
            @Override
            public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

            }

            @Override
            public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
                // 拿结果
                List<DrivePath> paths = driveRouteResult.getPaths();
                for (int i1 = 0; i1 < paths.size(); i1++) {
                    distan = paths.get(i1).getDistance();

                    Message m = new Message();
                    m.what=2;
                    Bundle b = new Bundle();
                    b.putString("dist", distan +"");
                    m.setData(b);
                    h.sendMessage(m);
                }

            }

            @Override
            public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

            }

            @Override
            public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

            }
        });



    }


    public void cryBook(final String id){
        // 提示抢单
        AlertDialog.Builder b = new AlertDialog.Builder(BookContentHotActivity.this);
        View view = LayoutInflater.from(BookContentHotActivity.this).inflate(R.layout.crybook_dialogitem, null);
        b.setView(view);
        Button no = (Button) view.findViewById(R.id.dialog_no);
        Button yes = (Button) view.findViewById(R.id.dialog_yes);
        final AlertDialog dialog = b.create();
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

                // 发送请求 提示成功
                RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.CRYBOOK);
                params.addHeader("Content-Type", "application/json");
                params.addHeader("token", token);
                params.addHeader("appId", appId);
                params.addHeader("userId", userId);
                JSONObject object = new JSONObject();
                object.put("orderId", id);
                params.setBodyContent(object.toJSONString());
                x.http().post(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        AlertDialog.Builder bu = new AlertDialog.Builder(BookContentHotActivity.this, R.style.NoBackGroundDialog);
                        View view1 = LayoutInflater.from(BookContentHotActivity.this).inflate(R.layout.crybook_okdialogitem, null);
                        bu.setView(view1);
                        Button okbt = (Button) view1.findViewById(R.id.dialog_okbt);
                        TextView message = (TextView) view1.findViewById(R.id.crybook_message);
                        final AlertDialog dialog1 = bu.create();
                        JSONObject obj1 = JSON.parseObject(result);
                        String status = obj1.getString("status");
                        if (status.equals("0")) {
                            String failure = obj1.getString("failure");
                            if (failure.equals("1028")) {
                                message.setText("您来晚了 订单已经被抢走了");
                            }else if(failure.equals("1011")){
                                message.setText("您提交的一喂员认证正在审核中，请耐心等待");
                            } else if (failure.equals("1029")) {
                                message.setText("为保证物品的安全,请申请一喂员,级别达到小工以上,才可以抢单");
                            } else if (failure.equals("1027")) {
                                message.setText("为保证物品的安全,请申请一喂员,级别达到小工以上,才可以抢单");
                            } else if (failure.equals("1030")) {
                                message.setText("此订单已您已经抢过了，再看看别的订单吧");
                            } else if (failure.equals("1032")) {
                                message.setText("订单已被对方取消，去看看别的订单吧");
                            } else if (failure.equals("1021")) {
                                message.setText("为保证物品的安全，级别达到小工以上才可以抢单 ");
                            } else if (failure.equals("10210")) {
                                message.setText("此单保价高于您的可接单额度(200),为保证物品的安全,需要达到小工级别以上才可以抢单 ");
                            } else if (failure.equals("10211")) {
                                message.setText("此单保价高于您的可接单额度(500),为保证物品的安全,需要达到贫农级别以上才可以抢单");
                            } else if (failure.equals("10212")) {
                                message.setText("此单保价高于您的可接单额度(1000),为保证物品的安全,需要达到富农级别以上才可以抢单");
                            } else if (failure.equals("10213")) {
                                message.setText("此单保价高于您的可接单额度(5000),为保证物品的安全,需要达到地主级别以上才可以抢单");
                            } else if (failure.equals("10214")) {
                                message.setText("此单保价高于您的可接单额度(1w),为保证物品的安全,需要达到知县级别以上才可以抢单");
                            } else if (failure.equals("10215")) {
                                message.setText("此单保价高于您的可接单额度(2w),为保证物品的安全,需要达到知府级别以上才可以抢单");
                            } else if (failure.equals("10216")) {
                                message.setText("此单保价高于您的可接单额度(5w),为保证物品的安全,需要达到总督级别以上才可以抢单");
                            } else if (failure.equals("10217")) {
                                message.setText("此单保价高于您的可接单额度(10w),为保证物品的安全,需要达到王爷级别以上才可以抢单");
                            } else {
                            }
                            okbt.setText("确定");
                            okbt.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog1.dismiss();
                                }
                            });
                        } else {
                            String data = obj1.getString("data");
                            JSONObject obj2 = JSON.parseObject(data);
                            String grabStatus = obj2.getString("grabStatus");

                            if (grabStatus.equals("0")) {
                                okbt.setText("确定");

                                message.setText("抢单成功，请等待选定吧");
                                okbt.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(BookContentHotActivity.this, OrderSelectActivity.class);
                                        startActivity(intent);
                                        dialog1.dismiss();
                                    }
                                });
                            } else {
                                okbt.setText("去取货");
                                message.setText("抢单成功，快上门取货吧");
                                okbt.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(BookContentHotActivity.this, OrderPickupActivity.class);

                                        startActivity(intent);
                                        dialog1.dismiss();
                                    }
                                });
                            }
                        }
                        dialog1.show();
                        dialog.dismiss();
                        MySqHelper helper = MySQManager.getInstance(BookContentHotActivity.this);
                        SQLiteDatabase db = helper.getWritableDatabase();
                        String where = "oid = ?";
                        db.delete("yivic", where, new String[]{id});
                        db.close();

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        ErrType.errType(BookContentHotActivity.this, ex);
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });

            }
        });
        dialog.show();
    }
}
